{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d4e0ca3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2.5\n",
      "1.21.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "print(pd.__version__)\n",
    "print(np.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dbf8c053",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "Name: x, dtype: int64\n",
      "\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "Name: x, dtype: int64\n",
      "\n",
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "dtype: int32\n",
      "\n",
      "b    2\n",
      "a    1\n",
      "c    3\n",
      "dtype: int64\n",
      "\n",
      "c    3\n",
      "a    1\n",
      "b    2\n",
      "dtype: int64\n",
      "\n",
      "0    5\n",
      "1    5\n",
      "2    5\n",
      "3    5\n",
      "dtype: int64\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "创建Series\n",
    "\n",
    "Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)\n",
    " |  data : array-like, Iterable, dict, or scalar value\n",
    " |      Contains data stored in Series. If data is a dict, argument order is\n",
    " |      maintained.\n",
    " |  index : array-like or Index (1d)\n",
    " |      Values must be hashable and have the same length as `data`.\n",
    " |      Non-unique index values are allowed. Will default to\n",
    " |      RangeIndex (0, 1, 2, ..., n) if not provided. If data is dict-like\n",
    " |      and index is None, then the keys in the data are used as the index. If the\n",
    " |      index is not None, the resulting Series is reindexed with the index values.\n",
    " |  name : str, optional\n",
    " |      The name to give to the Series.\n",
    "\"\"\"\n",
    "\n",
    "# 通过列表\n",
    "s = pd.Series([1, 2, 3], index=[\"a\", \"b\", \"c\"], name=\"x\")\n",
    "print(s)\n",
    "print()\n",
    "\n",
    "# 通过元组\n",
    "s = pd.Series((1, 2, 3), index=[\"a\", \"b\", \"c\"], name=\"x\")\n",
    "print(s)\n",
    "print()\n",
    "\n",
    "# 通过数组\n",
    "s = pd.Series(np.array([1, 2, 3]))\n",
    "print(s)\n",
    "print()\n",
    "\n",
    "# 通过字典\n",
    "s = pd.Series({\"b\": 2, \"a\": 1, \"c\": 3})\n",
    "print(s)\n",
    "print()\n",
    "\n",
    "s = pd.Series({\"b\": 2, \"a\": 1, \"c\": 3}, index=[\"c\", \"a\", \"b\"])\n",
    "print(s)\n",
    "print()\n",
    "\n",
    "# 通过标量，全部填充为标量值\n",
    "s = pd.Series(5, index=range(4))\n",
    "print(s)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "24e94344",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"csv文本文件的写入\"\"\"\n",
    "\n",
    "s = pd.Series([1, 2, 3], index=[\"a\", \"b\", \"c\"], name=\"x\")\n",
    "s.to_csv('2-data.csv', index=True, encoding='utf8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d9798dac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "   x\n",
      "a  1\n",
      "b  2\n",
      "c  3\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "   x\n",
      "a  1\n",
      "b  2\n",
      "c  3\n"
     ]
    }
   ],
   "source": [
    "\"\"\"csv文本文件的读取\"\"\"\n",
    "\n",
    "s_load = pd.read_csv('2-data.csv',\n",
    "                     sep = \",\",\n",
    "                     header = 'infer',\n",
    "                     index_col = 0,\n",
    "                     skiprows = 0\n",
    "                    )\n",
    "print(type(s_load))\n",
    "print(s_load)\n",
    "\n",
    "\n",
    "s_load = pd.read_table('2-data.csv',\n",
    "                     sep = \",\",\n",
    "                     header = 'infer',\n",
    "                     index_col = 0,\n",
    "                     skiprows = 0\n",
    "                    )\n",
    "print(type(s_load))\n",
    "print(s_load)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "75ca6cd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Excel文件的写入\"\"\"\n",
    "\n",
    "#定义Series\n",
    "s = pd.Series([1,2,3],index=[\"a\",\"b\",\"c\"],name=\"x\")\n",
    "\n",
    "#方式1\n",
    "s.to_excel(\"2-data.xlsx\", sheet_name=\"sheet1\", index=True)\n",
    "\n",
    "#方式2\n",
    "writer = pd.ExcelWriter(\"2-data2.xlsx\", mode=\"w\")\n",
    "s.to_excel(writer, sheet_name= \"sheet1\", index= True)\n",
    "s.to_excel(writer, sheet_name= \"sheet2\", index= True)\n",
    "writer.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3d7c234c",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"Excel文件的追加写入\"\"\"\n",
    "\n",
    "\n",
    "# 定义Series\n",
    "s = pd.Series([1, 2, 3], index=[\"a\", \"b\", \"c\"], name=\"x\")\n",
    "\n",
    "\n",
    "writer = pd.ExcelWriter(\"2-data2.xlsx\", mode=\"a\")\n",
    "s.to_excel(writer, sheet_name=\"sheet3\", index=True)\n",
    "writer.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4d3ef9e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   x\n",
      "a  1\n",
      "b  2\n",
      "c  3\n",
      "   xx\n",
      "a   1\n",
      "b   2\n",
      "c   3\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Excel文件的读取\"\"\"\n",
    "\n",
    "s = pd.read_excel(\"2-data2.xlsx\",\n",
    "                  sheet_name=0,\n",
    "                  index_col=0\n",
    "                  )\n",
    "print(s)\n",
    "\n",
    "\n",
    "s = pd.read_excel(\"2-data2.xlsx\",\n",
    "                  sheet_name=0,\n",
    "                  index_col=0,\n",
    "                  skiprows=1,\n",
    "                  header=None,\n",
    "                  names=[\"xx\"]\n",
    "                  )\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a9113c41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   x\n",
      "a  1\n",
      "b  2\n",
      "c  3\n"
     ]
    }
   ],
   "source": [
    "\"\"\"Excel文件的读取 方法2\"\"\"\n",
    "\n",
    "xlsx = pd.ExcelFile(\"2-data2.xlsx\")\n",
    "s = xlsx.parse(sheet_name=xlsx.sheet_names[0], index_col=0)\n",
    "print(s)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
